package com.hustzp.com.xichuangzhu.login;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.RequestMobileCodeCallback;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.XCZBaseFragmentActivity;
import com.hustzp.com.xichuangzhu.utils.DialogFactory;
import com.hustzp.com.xichuangzhu.utils.PhoneNumberTools;

public class ForgetPassWord extends XCZBaseFragmentActivity {
    private EditText inputPhonenumber;
    TextView tMiddle, tRight;
    ImageView   tLeftImage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pass_word);
        initView();

    }
    private   void initView(){
        inputPhonenumber = (EditText) findViewById(R.id.tv_phone_number);
        tMiddle = (TextView) findViewById(R.id.middle_text);
        tRight = (TextView) findViewById(R.id.right_text);
        tRight.setVisibility(View.GONE);
        tLeftImage = (ImageView) findViewById(R.id.left_Image);
        tMiddle.setText(getResources().getString(R.string.forget_password));
        tLeftImage.setOnClickListener(viewOnclick);
        findViewById(R.id.send_button).setOnClickListener(viewOnclick);
    }
    public View.OnClickListener viewOnclick=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int  viewID=  v.getId();
            switch (viewID){
                case R.id.left_Image:
                    ForgetPassWord.this.finish();
                    break;
                case  R.id.send_button:
                    sendPhonenumber();
                    break;
            }
        }
    };
    private void sendPhonenumber() {
        final String phoneNumbe = inputPhonenumber.getText().toString().trim();
        if (!PhoneNumberTools.verifyPhoneNumber(phoneNumbe)) {
            DialogFactory.hintDialog(this,getResources().getString(R.string.mainland_phonenumber));
            return;
        }

        final Dialog  loading = DialogFactory.createLoadingDialog(ForgetPassWord.this,"");
        AVOSCloud.requestSMSCodeInBackground(phoneNumbe, getResources().getString(R.string.app_name), getResources().getString(R.string.login_register),
                10, new RequestMobileCodeCallback() {
                    @Override
                    public void done(AVException e) {
                        loading.dismiss();
                        if (e == null) {
                            //存储注册的手机号
//                           user.setMobilePhoneNumber(phoneNumbe);
                          Intent  intent=  new Intent(ForgetPassWord.this,SmsCodeVerifyActivity.class);
                            intent.setAction("forgetPassWord");
                            startActivity(intent);
                        } else {
                            DialogFactory.hintDialog(ForgetPassWord.this,getResources().getString(R.string.sms_send_error));
                        }
                    }
                });
    }
}
